package com.iris.live.services.report.sources;

import com.iris.live.services.data.repositories.ReportRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by HangLiu on 2017/1/6.
 */
@Component("SmartWeekReportSource")
public class SmartWeekReportSource extends AbstractReportSourceService {

    private static final Logger logger = LoggerFactory.getLogger(LSHROSWeekReportSource.class);
    private static final String[] lastType = {"-CUSTOMER_ORDER_WEEKLY", "-CUSTOMER_ORDER_MTD", "-CUSTOMER_ORDER_YTD", "-CUSTOMER_ORDER_TOTAL_UNFULFILLED",
            "-RETAIL_SALE_WEEKLY", "-RETAIL_SALE_MTD", "-RETAIL_SALE_YTD",
            "-STOCK_WITH_ORDER", "-STOCK_WITHOUT_ORDER", "-STOCK_PENDING_FOR_INVOICE",
            "",
            "-tweJan", "-tweFeb", "-tweMar", "-tweApr", "-tweMay", "-tweJun", "-tweJul", "-tweAug", "-tweSep", "-tweOct", "-tweNov", "-tweDec",
            "", "-threeJan", "-threeFeb", "-threeMar", "-threeApr", "-threeMay", "-threeJun", "-threeJul", "-threeAug", "-threeSep", "-threeOct", "-threeNov", "-threeDec",
            "", "", "", "", "", "", "",
            "", "", "", "", "", "", "", "", "", "", "-WHOLESALE_NOTINTO_STORAGE", "-RETAIL_SALE_WITHOUT_GIVECAR"};

    private static final String[] indicators = {
            "smart-fortwo Coupe-C453-52KW COUPE",
            "smart-fortwo Coupe-C453-66KW COUPE",
            "smart-fortwo Cabrio-A453-52KW CABRIO",
            "smart-fortwo Cabrio-A453-66KW CABRIO",
            "smart-smart-smart-52KW COUPE PURE",
            "smart-smart-smart-52KW COUPE COMFORT",
            "smart-smart-smart-52KW COUPE",
            "smart-smart-smart-62KW COUPE",
            "smart-smart-smart-52KW CABRIO",
            "smart-smart-smart-62KW CABRIO",
            "smart-smart-smart-75KW XCLUSIVE COUPE",
            "smart-smart-smart-55KW COUPE ELECTRIC DRIVE",
            "smart-forfour-W453-FORFOUR 52KW",
            "smart-forfour-W453-FORFOUR 66KW",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "formular"
    };

    @Autowired
    private ReportRepository reportRepository;

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String date;
        if(context.containsKey("date")){
            date =context.get("date").toString();
        }else{
            Date now=new Date();
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
            date = sdf2.format(now);
        }
        Map values = getDataAboutWeekReport(date,sheet.getCode());
        List<Row> rows = new ArrayList<>();

        rows.add(new Row());
        rows.add(new Row());

        Cell[] cells = new Cell[1];
        cells[0] = new Cell(sheet.getName(), 7);
        rows.add(new Row(cells));

        //rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        rows.add(new Row());
        for (String indicator : indicators) {
            if (indicator == null || "".equals(indicator)) {
                rows.add(new Row());
            } else if (indicator.equals("formular")) {
                rows.add(new Row(true));
            } else {
                rows.add(new Row(getCells(values, indicator)));
            }
        }
        return rows;
    }

    private Cell[] getCells(Map values, String indicator) {
        Cell[] cells = new Cell[lastType.length];
        String value = null;
        for (int index = 0; index < lastType.length; index++) {
            if (!"".equals(lastType[index])) {
                Object o = values.get(indicator + lastType[index]);
                value = (o == null ? "0" : o).toString();
            } else {
                Object o1 = values.get(indicator + lastType[index - 2]);
                Object o2 = values.get(indicator + lastType[index - 1]);
                value = Double.parseDouble((o1 == null ? "0" : o1).toString()) + Double.parseDouble((o2 == null ? "0" : o2).toString()) + "";
            }
            cells[index] = new Cell(value, "double", 5 + index);
        }
        return cells;
    }

    private Map getDataAboutWeekReport(String date,String type) {
        Map map = new HashMap();
        try {
            List list = null;
            String[] types = type.split(",");
            if (types[0].equals("one")) {
                list = reportRepository.getWeekReportOne(types[1],date);
            } else {
                list = reportRepository.getWeekReport(type,date);
            }


            for (Object o : list) {
                Map m = (Map) o;
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_WEEKLY", m.get("CUSTOMER_ORDER_WEEKLY"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_MTD", m.get("CUSTOMER_ORDER_MTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_YTD", m.get("CUSTOMER_ORDER_YTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-CUSTOMER_ORDER_TOTAL_UNFULFILLED", m.get("CUSTOMER_ORDER_TOTAL_UNFULFILLED"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_WEEKLY", m.get("RETAIL_SALE_WEEKLY"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_MTD", m.get("RETAIL_SALE_MTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_YTD", m.get("RETAIL_SALE_YTD"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_WITH_ORDER", m.get("STOCK_WITH_ORDER"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_WITHOUT_ORDER", m.get("STOCK_WITHOUT_ORDER"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-STOCK_PENDING_FOR_INVOICE", m.get("STOCK_PENDING_FOR_INVOICE"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-ORDER_REQUIREMENT_PLAN", m.get("ORDER_REQUIREMENT_PLAN"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJan", m.get("oneJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneFeb", m.get("oneFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMar", m.get("oneMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneApr", m.get("oneApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneMay", m.get("oneMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJun", m.get("oneJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneJul", m.get("oneJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneAug", m.get("oneAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneSep", m.get("oneSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneOct", m.get("oneOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneNov", m.get("oneNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-oneDec", m.get("oneDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJan", m.get("tweJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweFeb", m.get("tweFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweMar", m.get("tweMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweApr", m.get("tweApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweMay", m.get("tweMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJun", m.get("tweJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweJul", m.get("tweJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweAug", m.get("tweAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweSep", m.get("tweSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweOct", m.get("tweOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweNov", m.get("tweNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-tweDec", m.get("tweDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJan", m.get("threeJan"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeFeb", m.get("threeFeb"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeMar", m.get("threeMar"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeApr", m.get("threeApr"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeMay", m.get("threeMay"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJun", m.get("threeJun"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeJul", m.get("threeJul"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeAug", m.get("threeAug"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeSep", m.get("threeSep"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeOct", m.get("threeOct"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeNov", m.get("threeNov"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-threeDec", m.get("threeDec"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-WHOLESALE_NOTINTO_STORAGE", m.get("WHOLESALE_NOTINTO_STORAGE"));
                map.put(m.get("SEGMENT") + "-" + m.get("MODEL_DE") + "-" + m.get("CLASSIS") + "-" + m.get("MODEL") + "-RETAIL_SALE_WITHOUT_GIVECAR", m.get("RETAIL_SALE_WITHOUT_GIVECAR"));

            }
        } catch (Exception e) {
            logger.error("周报表数据查询异常", e);
        }
        return map;
    }
}
